<?php
class SotaysController extends AppController {

	var $name = 'Sotays';
	var $helpers = array('Html', 'Form');
	var $paginate = array('order'=>array('NAM' => 'desc', 'Mua_Vue_id' => 'asc'));
	
	var $uses = array('Sotay', 'User', 'Trambvtv', 'NdDt', 'DtLoaidt', 'DiemDt', 'Ketqua', 'LoaiDt');

	function index($id=null) {
		if (!$id) {
			$this->Session->setFlash('Thao tác không hợp lệ');
			$this->redirect(array('controller'=>'Nonghos','action'=>'index1'));
		}
		
		$nongho = $this->Sotay->Nongho->find('first', array('conditions'=>array('Nongho.id'=>$id)));

		//$this->Sotay->recursive = 1;
		//$this->set('sotays', $this->paginate());
		
		$this->Sotay->recursive = 0;
		//if($this->Auth->user('ROLE')=='admin'){
			//$this->set('Sotays', $this->paginate('Sotay'));
		//}else{
	
		$cb = $this->User->find('first',array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
		$this->Trambvtv->recursive = 0;
		$tram = $this->Canbo->find('first',array('conditions'=>array('Canbo.id'=>$cb['Canbo']['id'])));
	//	$this->set('sotays', $this->paginate('Sotay',array('Sotay.trambvtv_id'=>$tram['Trambvtv']['id'])));
		$this->set('sotays', $this->paginate('Sotay',array('Sotay.nongho_id'=>$id)));
		//}
		$this->set(compact('nongho','tram','cb'));
	}
    
    function sotay_xls($id=null) {
		if (!$id) {
			$this->Session->setFlash('Thao tác không hợp lệ');
			$this->redirect(array('controller'=>'Nonghos','action'=>'index1'));
		}
		
		$nongho = $this->Sotay->Nongho->find('first', array('conditions'=>array('Nongho.id'=>$id)));

		//$this->Sotay->recursive = 1;
		//$this->set('sotays', $this->paginate());
		
		$this->Sotay->recursive = 2;
		//if($this->Auth->user('ROLE')=='admin'){
			//$this->set('Sotays', $this->paginate('Sotay'));
		//}else{
	
		$cb = $this->User->find('first',array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
		$this->Trambvtv->recursive = 2;
		$tram = $this->Canbo->find('first',array('conditions'=>array('Canbo.id'=>$cb['Canbo']['id'])));
	//	$this->set('sotays', $this->paginate('Sotay',array('Sotay.trambvtv_id'=>$tram['Trambvtv']['id'])));
		$this->set('sotays', $this->paginate('Sotay',array('Sotay.nongho_id'=>$id)));
		//}
       
		$this->set(compact('nongho','tram','cb'));
        $this->layout='';
	}
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Sotay.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('sotay', $this->Sotay->read(null, $id));
	}


	function xuatpdf($id = null) {
		if (!$id) {
			$this->Session->setFlash('Thao tác không hợp lệ');
			$this->redirect(array('controller'=>'pages', 'action'=>'home'));
		}
		
		App::import('Vendor', 'xtcpdf');
		 
		$mypdf = new XTCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
		$this->layout = null;
		
		$mypdf->init();
		$mypdf->AddPage();

		$sotay = $this->Sotay->find('first', array('conditions'=>array('Sotay.id'=>$id)));	
		$this->set('sotayId', $id);
		$this->set(compact('sotay'));
		$mypdf->writeHTML('<b>I.&nbsp;THÔNG TIN CHUNG</b>', true, false, false, false, 'L');
		$mypdf->writeHTML('<b>&nbsp;&nbsp;&nbsp;1.&nbsp;Cơ cấu giống</b>', true, false, false, false, 'L');
		$mypdf->Cell(30 ,7 ,'* Giống Lúa: '.$sotay['Gionglua']['TEN_GL'], 0,1, 'L',0);
		$mypdf->Cell(30 ,7 ,'* Khả năng chống chịu: '.$sotay['Gionglua']['KNCC'], 0,1, 'L',0);
		$mypdf->Cell(30 ,7 ,'* Thời gian gieo cấy: '.date('d-m-Y', strtotime( $sotay['Sotay']['TGGC'])), 0,1, 'L',0);
//
		$mypdf->writeHTML('<b>&nbsp;&nbsp;&nbsp;2.&nbsp;Tình hình sử dụng phân bón</b>', true, false, false, false, 'L');
		// Lấy dữ liệu
	
		
		//su dung phan
		$sdPhanbons = $this->Sotay->SdPhanbon->find('all', array('conditions'=>array('SdPhanbon.Sotay_id'=>$id)));
		$this->set(compact('sdPhanbons'));	
			
		$mypdf->writeHTML($mypdf->printsdPT($sdPhanbons), true, false, false, false, '');
		
		
		$mypdf->writeHTML('<b>&nbsp;&nbsp;&nbsp;3.&nbsp;Tình hình sử dụng thuốc</b>', true, false, false, false, 'L');
		//su dung thuoc
		$sdungThuocs = $this->Sotay->SdungThuoc->find('all', array('conditions'=>array('SdungThuoc.Sotay_id'=>$id)));
		$this->set(compact('sdungThuocs'));
		
		$mypdf->writeHTML($mypdf->printsdThuoc($sdungThuocs), true, false, false, false, '');
		
		
		//XUAT CAC PHIEU DIEU TRA

		$phieudts = $this->Sotay->PhieuDt->find('all',array('conditions'=>array('PhieuDt.Sotay_id'=>$id)));
		
		//chay tung phieu dieu tra
		for($ipdt = 0; $ipdt < count($phieudts); $ipdt++){
			$mypdf->AddPage();
	
			//neu la trang dau tien cua phieu dieu tra thi hien ra tieu de II
			if 	($ipdt == 0)
				$mypdf->writeHTML('<b>II.&nbsp;THÔNG TIN ĐIỀU TRA</b>', true, false, false, false, 'L');
				
			$phieudt = $phieudts[$ipdt];
			
			$mypdf->Cell(30 ,7 ,'* Cán bộ điều tra: '.$phieudt['Canbo']['HOTEN'], 0,1, 'L',0);
			$mypdf->Cell(30 ,7 ,'* Ngày điều tra: '.date('d-m-Y', strtotime($phieudt['PhieuDt']['NGAY'])).' (Lần thứ:'.$phieudt['PhieuDt']['LAN'].')', 0,1, 'L',0);
			$mypdf->Cell(30 ,7 ,'* Giai đoạn sinh trưởng(NSS): '.$phieudt['Nss']['TEN_NSS'], 0,1, 'L',0);
	
			$loaidts = $this->LoaiDt->find('all', array('conditions'=>array('LoaiDt.TEN_LOAI !='=>'Bẫy đèn'),'fields'=>'DISTINCT LoaiDt.*','order'=>'LoaiDt.id asc'));
			$i = 1;
			
			//foreach($loaidts as $loaidt){			
			for($ildt = 0; $ildt < count($loaidts); $ildt++){
				//xuat LOAI DOITUONG
				$loaidt = $loaidts[$ildt];

				$mypdf->Cell(30 ,7 ,'   '.$i++.'. '.$loaidt['LoaiDt']['TEN_LOAI'], 0, 0, 'L',0);
				
				//xuong dong
				$mypdf->Ln();
			
				$this->NdDt->recursive = 0;
				$noidungs = $this->NdDt->find('all', array('conditions'=>array('loai_dt_id'=>$loaidt['LoaiDt']['id']), 'order'=>'NdDt.id', 'fields'=>'DISTINCT NdDt.*'));
			
				$this->DtLoaidt->recursive = 2;
				$doituongs = $this->DtLoaidt->find('all', array('conditions'=>array('DtLoaidt.loai_dt_id'=>$loaidt['LoaiDt']['id']), 'order'=>'DtLoaidt.Doituong_id', 'fields'=>'DISTINCT Doituong.*'));
			
				$this->DiemDt->recursive = 0;
				$diems = $this->DiemDt->find('all', array('order'=>'DIEM', 'fields'=>'DISTINCT *'));
			
				$this->Ketqua->recursive = 1;
				$ketquas = $this->Ketqua->find('all',array('conditions'=>array('NdDt.loai_dt_id'=>$loaidt['LoaiDt']['id'],'PhieuDt.id'=>$phieudt['PhieuDt']['id']),'order'=>'Doituong.id, DiemDt.DIEM, NdDt.id','fields'=>'DISTINCT Ketqua.*'));

				$mypdf->writeHTML($mypdf->printPdts($noidungs,$doituongs,$diems,$ketquas,$phieudt), true, false, false, false, '');
			}
			
		}
		
		$mypdf->Output('sotay.pdf', 'D');
	}




	function add($nonghoId) {
		if (!empty($this->data)) {
			$this->Sotay->create();
			if ($this->Sotay->save($this->data)) {
				$this->Session->setFlash('Sổ tay đã được lưu');
				$this->redirect(array('action'=>'index', $nonghoId));
			} else {
				$this->Session->setFlash('Lỗi lưu dữ liệu <br/> Có thể giống lúa ở mùa vụ năm này đã được lưu.');
			}
		}
		$sotay = $this->Sotay->find('first',array('conditions'=>array('nongho_id',$nonghoId)));//moi sua
		$giongluas = $this->Sotay->Gionglua->find('list',array('fields'=>array('Gionglua.id','Ten_gl')));//moi sua
		$muaVues = $this->Sotay->MuaVue->find('list',array('fields'=>array('MuaVue.id','Ten_mua')));//moi sua
		$nams = $this->Sotay->Nam->find('list',array('fields'=>array('Nam.id','Nam')));//moi sua
			
		$cb = $this->User->find('first',array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
		$this->Trambvtv->recursive = 1;
		$tram = $this->Canbo->find('first',array('conditions'=>array('Canbo.id'=>$cb['Canbo']['id'])));
		
		$this->set(compact('nonghoId', 'giongluas', 'muaVues', 'nams', 'sotay','trambvtv','tram','cb'));// 'nonghos',
	}

	function edit($nonghoId=null,$id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Sổ tay không hợp lệ', true));
			$this->redirect(array('action'=>'index',$nonghoId));
		}
		if (!empty($this->data)) {
			if ($this->Sotay->save($this->data)) {
				$this->Session->setFlash(__('Sổ tay đã lưu', true));
                $nonghoId=$this->data['Sotay']['nongho_id'];
				$this->redirect(array('action'=>'index',$nonghoId));
			} else {
				$this->Session->setFlash(__('Không lưu được. Thử lại lần nữa.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Sotay->read(null, $id);
		}
		$nonghos = $this->Sotay->Nongho->find('list',array('fields'=>array('Nongho.id','Ten_nh')));//moi sua
		$giongluas = $this->Sotay->Gionglua->find('list',array('fields'=>array('Gionglua.id','Ten_gl')));//moi sua
		$muaVues = $this->Sotay->MuaVue->find('list',array('fields'=>array('MuaVue.id','TEN_MUA')));//moi sua
		$nams = $this->Sotay->Nam->find('list',array('fields'=>array('Nam.id','Nam')));//moi sua
		$sotay = $this->Sotay->find('first', array('conditions'=>array('Sotay.id'=>$id)));
		
//		$cbid = $this->Auth->user('canbo_id');
        $cb = $this->User->find('first',array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
		$trambvtv = $this->Canbo->find('first', array('conditions'=>array('Canbo.id'=>$cb['User']['canbo_id'])));
				
		$this->set(compact('nonghos','giongluas','muaVues','nams','trambvtv', 'sotay','id','nonghoId'));
	}

	function delete($id = null) {		
		if (!$id) {
			$this->Session->setFlash(__('Sổ tay không hợp lệ', true));
			$this->redirect(array('action'=>'index'));
		}
		
		$this->Sotay->recursive = 0;
		$sotay = $this->Sotay->findById($id);//'first', array('condition'=>array('Sotay.id'=>$id)));
	
		$temp1=$sotay['Sotay']['nongho_id'];
		//$this->set(compact('sotay','temp1'));
		
		if ($this->Sotay->del($id)) {
	//		$this->Session->setFlash(__('Sổ tay đã được xóa'.$id.$sotay['Sotay']['nongho_id'], true));
			$this->Session->setFlash(__('Sổ tay đã được xóa', true));
			$this->redirect(array('action'=>'index',$sotay['Sotay']['nongho_id']));
		}
	
	}


	function tinh_index() {
		$this->Sotay->recursive = 0;
		$this->set('sotays', $this->paginate());
	}

	function tinh_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Sotay.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('sotay', $this->Sotay->read(null, $id));
	}

	function tinh_add() {
		if (!empty($this->data)) {
			$this->Sotay->create();
			if ($this->Sotay->save($this->data)) {
				$this->Session->setFlash(__('The Sotay has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Sotay could not be saved. Please, try again.', true));
			}
		}
		$nonghos = $this->Sotay->Nongho->find('list');
		$giongluas = $this->Sotay->Gionglua->find('list');
		$muaVues = $this->Sotay->MuaVue->find('list');
		$nams = $this->Sotay->Nam->find('list');
		$this->set(compact('nonghos', 'giongluas', 'muaVues', 'nams'));
	}

	function tinh_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Sotay', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Sotay->save($this->data)) {
				$this->Session->setFlash(__('The Sotay has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Sotay could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Sotay->read(null, $id);
		}
		$nonghos = $this->Sotay->Nongho->find('list');
		$giongluas = $this->Sotay->Gionglua->find('list');
		$muaVues = $this->Sotay->MuaVue->find('list');
		$nams = $this->Sotay->Nam->find('list');
		$this->set(compact('nonghos','giongluas','muaVues','nams'));
	}

	function tinh_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Sotay', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Sotay->del($id)) {
			$this->Session->setFlash(__('Sotay deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>